﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Administration;
using System.Collections.Generic;

namespace BlobCacheManager.Layouts.BlobCacheManager
{
    public partial class BlobCacheManager : LayoutsPageBase
    {
        string webAppId;
        protected void Page_Load(object sender, EventArgs e)
        {
            webAppId = Request.QueryString["ID"];
            if (webAppId == null || webAppId == "")
            {
                throw new SPException("Specify the Web Application ID");
            }
            else
            {
                try
                {
                    SPWebApplication oWebApp = SPFarm.Local.GetObject(new Guid(webAppId)) as SPWebApplication;
                    if (oWebApp == null)
                    {
                        throw new SPException("The Web Application provided is not valid.");
                    }
                    else
                    {
                        if (!IsPostBack)
                        {
                            BCManager oBCManager = new BCManager(webAppId);
                            Dictionary<string, string> currentMods = oBCManager.GetCurrentMods();
                            try { EnabledCheckBox.Checked = Boolean.Parse(currentMods["enabled"]); }
                            catch (KeyNotFoundException) { EnabledCheckBox.Checked = false; }
                            try { MaxSizeTextBox.Text = currentMods["maxSize"]; }
                            catch (KeyNotFoundException) { MaxSizeTextBox.Text = "10"; }
                            try { LocationTextBox.Text = currentMods["location"]; }
                            catch (KeyNotFoundException) { LocationTextBox.Text = @"C:\BlobCache\14"; }
                            try { MaxAgeTextBox.Text = currentMods["max-age"]; }
                            catch (KeyNotFoundException) { MaxAgeTextBox.Text = "86400"; }
                            try
                            {
                                string path = currentMods["path"];
                                //replace the extension dot and the opening brace
                                path = path.Replace("\\.(", "");
                                //replace the end of the regex and the closing brace
                                path = path.Replace(")$", "");
                                PathTextBox.Text = path;
                            }
                            catch (KeyNotFoundException) { PathTextBox.Text = "gif|jpg|jpeg|jpe|jfif|bmp|dib|tif|tiff|ico|png|wdp|hdp|css|js|asf|avi|flv|m4v|mov|mp3|mp4|mpeg|mpg|rm|rmvb|wma|wmv"; }
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new SPException(ex.Message);
                }
            }
        }

        protected void Btn_Ok_Click(object sender, EventArgs e)
        {
            using (SPLongOperation longOperation = new SPLongOperation(this.Page))
            {
                longOperation.LeadingHTML = "Applying settings";
                longOperation.TrailingHTML = "Please wait while the settings are being applied.";
                longOperation.Begin();
                string script = "window.frameElement.commonModalDialogClose(0, 1);";
                try
                {
                    bool enabledCheckBox = EnabledCheckBox.Checked;
                    int maxSize = Int32.Parse(MaxSizeTextBox.Text);
                    string location = LocationTextBox.Text;
                    int maxAge = Int32.Parse(MaxAgeTextBox.Text);
                    string path = PathTextBox.Text;
                    if (location.EndsWith(@"\"))
                    {
                        location = location.TrimEnd('\\');
                    }
                    location = location.Trim();
                    BCManager oBCManager = new BCManager(enabledCheckBox, maxSize, location, maxAge, path, webAppId);
                    oBCManager.UpdateChanges();
                    script = "window.frameElement.commonModalDialogClose(1, 1);";
                }
                catch (Exception ex)
                {
                    throw new SPException(ex.Message);
                }
                finally
                {
                    longOperation.EndScript(script);
                }
            }
        }
    }
}
